/* Copyright (c) [2025] [zhi1208@126.com]
 * SPDX-License-Identifier: MIT
 */

/*********************
 *      INCLUDES
 *********************/
#include "lvgl.h"
#include "hmi_pg_def.h"
#include "hmi_config.h"
#include "hmi_anim.h"
#include "hmi_png.h"
#include "hmi_tinyttf.h"
#include "hmi_pg_navigation.h"
#include "hmi_pages.h"

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/
static void create();
static void del();
static void enter_anim(lv_anim_completed_cb_t cb, uint32_t delay);
static uint32_t exit_anim(lv_anim_completed_cb_t cb, hmi_pg_t *enter_pg);

static void back_btn_click_cb(lv_event_t *e);
/**********************
 *  STATIC VARIABLES
 **********************/
static hmi_pg_t fail = {
    .name = "连接失败",
    .bg_color = 0xe03e36,
    .del_on_pop = 1,
    .create = create,
    .del = del,
    .enter_anim = enter_anim,
    .exit_anim = exit_anim};

static lv_font_t *def_font_18;
static lv_font_t *def_font_20;

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
 **********************/
hmi_pg_t *hmi_pg_fail()
{
    return &fail;
}

/**********************
 *   STATIC FUNCTIONS
 **********************/
static void create()
{
    fail.bg_y = HMI_BG_FULL;

    fail.root = lv_obj_create(NULL);
    lv_obj_remove_style_all(fail.root);

    if (!def_font_18)
        def_font_18 = hmi_tiny_ttf_create_static("def_font", 18, 0);

    if (!def_font_20)
        def_font_20 = hmi_tiny_ttf_create_static("def_font", 20, 0);

    lv_obj_t *cont = lv_obj_create(fail.root);
    lv_obj_remove_style_all(cont);
    lv_obj_set_size(cont, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
    lv_obj_set_style_pad_row(cont, 10, 0);
    lv_obj_set_flex_flow(cont, LV_FLEX_FLOW_COLUMN);
    lv_obj_set_flex_align(cont, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
    lv_obj_align(cont, LV_ALIGN_CENTER, 0, -20);

    lv_obj_t *ok_img = lv_image_create(cont);
    lv_image_set_src(ok_img, hmi_png("xmark_circle_fill"));

    lv_obj_t *hint = lv_label_create(cont);
    lv_obj_set_style_text_color(hint, lv_color_white(), 0);
    lv_obj_set_style_text_font(hint, def_font_18, 0);
    lv_label_set_text(hint, "连接失败，请重试");

    lv_obj_t *back_btn = lv_button_create(fail.root);
    lv_obj_set_size(back_btn, lv_pct(35), LV_SIZE_CONTENT);
    lv_obj_set_style_radius(back_btn, LV_RADIUS_CIRCLE, 0);
    lv_obj_set_style_border_color(back_btn, lv_color_white(), 0);
    lv_obj_set_style_border_width(back_btn, 2, 0);
    lv_obj_set_style_shadow_width(back_btn, 0, 0);
    lv_obj_set_style_bg_opa(back_btn, LV_OPA_TRANSP, 0);
    lv_obj_align_to(back_btn, cont, LV_ALIGN_OUT_BOTTOM_MID, 0, 40);
    lv_obj_add_event_cb(back_btn, back_btn_click_cb, LV_EVENT_CLICKED, NULL);

    lv_obj_t *back_label = lv_label_create(back_btn);
    lv_obj_set_style_text_color(back_label, lv_color_white(), 0);
    lv_obj_set_style_text_font(back_label, def_font_20, 0);
    lv_label_set_text(back_label, "返回");
    lv_obj_center(back_label);
}

static void del()
{
    lv_obj_delete_async(fail.root);
    fail.root = NULL;

    if (def_font_18)
    {
        hmi_tiny_ttf_del(def_font_18);
        def_font_18 = NULL;
    }

    if (def_font_20)
    {
        hmi_tiny_ttf_del(def_font_20);
        def_font_20 = NULL;
    }
}

static void enter_anim(lv_anim_completed_cb_t cb, uint32_t delay)
{
    hmi_enter_anim_default(fail.root, cb, delay);
}

static uint32_t exit_anim(lv_anim_completed_cb_t cb, hmi_pg_t *enter_pg)
{
    return hmi_exit_anim_default(fail.root, cb, enter_pg);
}

static void back_btn_click_cb(lv_event_t *e)
{
    LV_UNUSED(e);
    hmi_back_to(hmi_pg_wifi());
}